<?php

namespace app\commands;

use app\models\Sms;
use app\models\user\User;
use app\models\user\UserAccount;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\db\Exception;

class FlushController extends Controller
{
    // php yii flush/index
    public function actionIndex($add_user_num = 1)
    {
        $add_user_num = intval($add_user_num);

        $this->clear('sms');
        $this->clear('admin_ops_refill');

        $this->clear('order');
        $this->clear('order_item');
        $this->clear('order_logistics');
        $this->clear('order_offline');
        // $this->clear('order_store');
        // $this->clear('order_store_extract');

        $this->clear('pay');
        $this->clear('pay_for');

        $this->clear('user');
        $this->clear('user_account');

        $this->clear('user_address');
        $this->clear('user_pay_password');
        $this->clear('user_balance');

        $this->clear('user_store');
        $this->clear('user_store_log');
        $this->clear('user_reg_area');

        $this->clear('user_bank');
        $this->clear('user_cart');
        $this->clear('user_refill');
        $this->clear('user_sign_in');
        $this->clear('user_commission');
        $this->clear('user_consume');
        $this->clear('user_cash');
        $this->clear('user_fav');

        $this->clear('admin_ops_refill');

        $this->clear('group');
        $this->clear('group_fans');
        $this->clear('group_list');

        $this->clear('transfer');

        $this->clear('queue');
        $this->clear('comment');
        $this->clear('comment_image');

        // 一定要留一个, 否则ID会从1开始
        $my_id = 11194;
        $my_pass = '222222';
        for ($i = 0 ; $i < $add_user_num; $i++) {
            $model = new User();
            $model->nickname = base64_encode('测试会员');
            $model->mobile = strval(18620885098 + $i);
            $model->password = $my_pass;
            if ($i == 0) {
                $model->id = strval($my_id);
            }
            if ($i < 10 - 1) {
                $model->referrer_id = strval($my_id + $i + 1);
            }
            if ($model->save()) {
                $account = new UserAccount();
                $account->user_id = $model->id;
                if ($i == 0) {
                    // $account->balance = 8888888;
                    $account->balance = 0;
                }
                $account->save();
            }
        }

        return ExitCode::OK;
    }

    public function actionProduct()
    {
        $this->clear('images');
        $this->clear('product');
        $this->clear('product_category');
        $this->clear('product_image');
        $this->clear('product_property');
        $this->clear('product_area_list');
        $this->clear('product_sku');
        $this->clear('product_sku_name');
        $this->clear('product_sku_value');

        $this->clear('article');
        $this->clear('article_category');
        $this->clear('article_fav');
    }

    private function clear($table_name)
    {
        try {
            echo 'Clear ' . $table_name . ' ................. done!', "\n";
            Sms::getDb()->createCommand()->truncateTable($table_name)->execute();
        } catch (Exception $e) {
            echo 'error: ' . $e->getMessage(), "\n";
        }
    }
}